<template>
  <div class="content">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>条件查询</span>
      </div>
      <!-- 搜索输入框 -->
      <el-row>
        <el-col :span="6">
          <div class="input-holder">
            <span>订单编号</span>
            <el-input
              v-model="searchVal.orderSn"
              placeholder="请输入标题"
              clearable
              size="small"
            />
          </div>
        </el-col>
        <el-col :span="6">
          <span>订单类型</span>
          <el-select v-model="searchVal.sourceType " size="small" placeholder="请选择订单类型">
            <el-option
              v-for="item in sourceTypeOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-col>
        <el-col :span="6">
          <span>支付方式</span>
          <el-select v-model="searchVal.payType " size="small" placeholder="请选择支付方式">
            <el-option
              v-for="item in payTypeOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-col>
        <el-col :span="6">
          <span>订单状态</span>
          <el-select v-model="searchVal.status " size="small" placeholder="请选择订单状态：">
            <el-option
              v-for="item in statusOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-col>
      </el-row>
      <el-row>
        <!-- 搜索按钮 -->
        <el-col :offset="20" :span="4" style="margin-top:20px">
          <el-button type="primary" size="small" @click="search">搜索</el-button>
          <el-button size="small" @click="resetSearch">重置</el-button>
        </el-col>
      </el-row>
    </el-card>

    <el-card shadow="always" :body-style="{ padding: '20px' }">

      <div class="table-holder">
        <!-- 文章展示表格 -->
        <el-table :data="orderList" border style="width: 100%">
          <el-table-column fixed="left" type="index" label="#" width="50" />
          <el-table-column prop="orderSn" label="订单编号" width="200" />
          <el-table-column label="支付方式" width="200">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.payType === 0" type="info" effect="dark">
                未支付
              </el-tag>
              <el-tag v-if="scope.row.payType === 1" effect="dark">
                <img src="../../../icons/png/alipay.png" alt="">
              </el-tag>
              <el-tag v-if="scope.row.payType === 2" type="success" effect="dark">
                <img src="../../../icons/png/wx.png" alt="">
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column label="订单状态" width="200">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.status === 0" type="danger" effect="dark">
                待付款
              </el-tag>
              <el-tag v-if="scope.row.status === 1" type="warning" effect="dark">
                待发货
              </el-tag>
              <el-tag v-if="scope.row.status === 2">
                已发货
              </el-tag>
              <el-tag v-if="scope.row.status === 3" effect="dark">
                已完成
              </el-tag>
              <el-tag v-if="scope.row.status === 4" type="info" effect="dark">
                无效订单
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="createTime" label="添加时间" width="200" />
          <el-table-column prop="memberUsername" label="用户账号" width="200" />
          <el-table-column label="订单类型" width="200">
            <template slot-scope="scope">
              <el-tag v-if="scope.row.orderType === 1" type="danger">
                秒杀订单
              </el-tag>
              <el-tag v-else type="success">
                正常订单
              </el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="note" label="备注" width="200" />
          <el-table-column fixed="right" label="操作" width="200">
            <template slot-scope="scope">
              <el-button
                type="primary"
                size="small"
                icon="el-icon-view"
                @click="toOrderInfo(scope.row.id)"
              >查看订单</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 分页器 -->
      <div class="pagination-holder">
        <el-pagination
          :current-page="page.start"
          :page-sizes="page.pageSize"
          :page-size="page.limit"
          layout="total, sizes, prev, pager, next, jumper"
          :total="page.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script>
import {
  requestOrderList
} from '@/api/order'
import mixin from '@/mixins/index'
export default {
  mixins: [mixin],
  data() {
    return {
      // 订单列表
      orderList: [],
      // 订单搜索内容
      searchVal: {
        'deliverySn': '',
        'id': '',
        'memberUsername': '',
        'orderSn': '',
        'orderType': '',
        'payType': '',
        'sourceType': '',
        'status': ''
      },
      // 选择订单类型
      sourceTypeOptions: [{
        value: 0,
        label: 'PC订单'
      },
      {
        value: 1,
        label: 'app订单'
      }],
      payTypeOptions: [{
        value: 0,
        label: '未支付'
      },
      {
        value: 1,
        label: '支付宝'
      }, {
        value: 2,
        label: '微信'
      }],
      statusOptions: [{
        value: 0,
        label: '待付款'
      },
      {
        value: 1,
        label: '待发货'
      }, {
        value: 2,
        label: '已发货'
      },
      {
        value: 3,
        label: '已完成'
      },
      {
        value: 4,
        label: '已关闭'
      },
      {
        value: 5,
        label: '无效订单'
      }]
    }
  },
  created() {
    this.getData()
  },
  methods: {
    // 重置搜索结果
    resetSearch() {
      this.searchVal = {
        'deliverySn': '',
        'id': '',
        'memberUsername': '',
        'orderSn': '',
        'orderType': '',
        'payType': '',
        'sourceType': '',
        'status': ''
      }
      this.getData()
    },
    // 搜索
    search() {
      this.page.start = 0
      this.getData()
    },
    // 获取文章列表信息
    getData() {
      requestOrderList(this.page, this.removeBlock(this.searchVal)).then((res) => {
        console.log(res)
        this.page.total = res.data.total
        this.orderList = res.data.rows
      })
    },
    // 跳转到订单详情
    toOrderInfo(id) {
      this.$router.push({ name: 'OrderDetail', params: { id }})
    },
    // 改变每页数量
    handleSizeChange(val) {
      this.page.limit = val
      this.getData()
    },
    // 改变页码
    handleCurrentChange(val) {
      this.page.start = val
      this.getData()
    }
  }
}
</script>

@import '@/style/index.scss'

<style lang="scss" scoped>
.content {
  .image {
    width: 80%;
    height: 80%;
  }

  .input-holder{
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 80%;

    span{
      width: 90px;
    }
  }
}
</style>
